package com.lyq.leetcode.无重复字符的最长子串3;

import java.util.HashMap;

public class Main {
    public static void main(String[] args) {
        Main main = new Main();
        System.out.println(main.lengthOfLongestSubstring("au"));
    }

    public int lengthOfLongestSubstring(String s) {
        HashMap<Character, Integer> map = new HashMap<>(s.length());
        char[] charArray = s.toCharArray();
        if (charArray.length <= 1) {
            return charArray.length;
        }
        int res = 0;
        int slow = 0;
        int quick = 1;
        map.put(charArray[0], 0);
        while (quick < charArray.length) {
            if (map.containsKey(charArray[quick]) && map.get(charArray[quick])>=slow) {
                res = Math.max(res, quick - slow);
                slow = map.get(charArray[quick]) + 1;
            }
            map.put(charArray[quick], quick);
            quick++;
        }
        res = Math.max(res, quick - slow -1);
        return res;
    }
}
